R-Shiny: Select 输入对文件输入有反应

R-Shiny: Select input reactive on file input

我对 Shiny 很陌生,不确定我是否在远程执行此操作 correct/completely 过于简单化。我正在尝试将 headers 列从 excel fileInput 拉到 selectInput 下拉框中。 所以基本上我希望 select 框的选项由文件输入的 headers 确定。然后它会 link 进入我在服务器中的等式,这将根据列中的数据集(服务器中带有 input$col 的位)执行计算。 我感谢任何 comments/answers, 谢谢

编辑:我猜,我需要使用 uiOutput 和 renderUI 吗??

ui

 ui <- fluidPage(theme = shinytheme(),

setBackgroundColor("white"),

titlePanel(img(src = "image.png", height = 125, width = 450)),

(h1("review app", style = "color:#337ab7")),
p("Calculate"),

headerPanel(h3("Input data here", style = "color:#337ab7")), 


sidebarLayout(
sidebarPanel( position =c("left"),  style = "color:#337ab7", 
    numericInput("SL",
                "SL", 1, min=1, max=10),

    numericInput("LT", "LT",0, min=0, max = 52),
    fileInput("file1", 'choose file',
              accept = c(".xlsx") ),
    selectInput("col", "Column", choices = unique(colnames(input$file1)
                                                   )),

   checkboxInput("smooth", "Clean my data", value = FALSE, width = NULL),

    actionButton("action_Calc", label = "Refresh & Calculate", icon("redo"), 
         style="color: #fff; background-color: #337ab7; border-color: #2e6da4"), 
     ),


mainPanel(
    tabsetPanel(
      tabPanel("SS", h1(textOutput("SS"), style = "color:#337ab7")),
      tabPanel("guide",  img(src = "guide.png", height = 200, width = 600)),
      tabPanel("Mydata", div(tableOutput('contents'), style="font-size:55%"))
          ))))

服务器

 server <- function(input, output) {


  Data <- reactive({
  req(input$file1)
  inFile <- input$file1
  read_excel(inFile$datapath, 1)
})

output$contents <- renderTable(bordered = TRUE, style= "border-color:#337ab7", hover = TRUE, {
  Data()
})


values<- reactiveValues()
observe({
    input$action_Calc
    values$int<- isolate({ if (input$smooth) (round( input$SL*sqrt(input$LT/4)*sd( tsclean(Data()[[input$col]], 
       replace.missing = TRUE, lambda = NULL)) , digits= 2))
       else (round( input$SL*sqrt(input$LT/4)*sd(Data()[[input$col]]), digits = 2)) })})

    output$SS <- renderText({paste("Calculated is", values$int)} )

} shinyApp(ui,服务器)

updatedSelectInput 应该为您完成。下面是一个最小的例子。

为了减少包依赖性,我改为加载 .csv 而不是 .xlsx。请注意,加载的文件未经验证,因此如果垃圾进入,您将得到垃圾。

library(shiny)

#UI
ui <- fluidPage(

    selectInput('mydropdown', label = 'Select', choices = 'No choices here yet'),

    fileInput('myfileinput', label = 'Select File', accept = c(".csv"))

)

#Server
server <- function(input, output, session) {

    observeEvent(input$myfileinput, {

        mytable <- read.csv(input$myfileinput$datapath)

        updateSelectInput(session, "mydropdown", label = "Select", choices = colnames(mytable))

    })

}

shinyApp(ui = ui, server = server)