繁体   English   中英

Shiny 用反应操纵 input$df

[英]Shiny manipulating input$df with reactive

我是 shiny 的新手,并尝试修改此代码以允许从动态选择的 csv 中使用 function。

## function to return random row number from data set
getTweet <- function(){
 tweetData[sample(nrow(tweetData), 1), ]
}

our_tweet <- isolate(appVals$tweet)
  output$tweet <- renderText({ our_tweet$tweet })
  output$screen_name <- renderText({ our_quote$screen_name })
  output$resultsTable <- renderDataTable({appVals$ratings})

The above code works when tweetData is a static csv read in through read.csv() but when I try to use a drop down to select csv the only way I am able to run without error is putting it inside of a renderDataTable() function . 我如何在 input$file 中使用反应值并且仍然能够运行上述代码。

使用 renderDataTable() 的代码:

 output$test <- renderDataTable({
    req(input$file)
    csvName <- paste0('../path/to/file/', input$file)
    selectedData <- read.csv(csvName)
    selectedData
    })

我希望能够做这样的事情:

csvName <- paste0('../path/to/file/', input$file)
selectedData <- read.csv(csvName)

selectedData[sample(nrow(selectedData), 1), ]

您可以创建reactive函数:

csvName <- reactive(paste0('../path/to/file/', input$file))
selectedData <- reactive(read.csv(csvName()))

然后,您可以在其他响应式中使用响应式函数,例如renderDataTable

output$test <- renderDataTable({
  selectedData()[sample(nrow(selectedData()), 1), ]
    })

调用响应式函数的结果时不要忘记()csvName() , selectedData() , , ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM