Я очень новичок в Shiny и изо всех сил пытаюсь понять реактивность.
Контекст: я хочу, чтобы пользователь выбрал имя для столбца, добавил этот столбец в реактивную таблицу и затем отредактировал эту таблицу. Таблица является реагирующей (она поступает из загруженного файла, отфильтрованного пользователем).
Благодаря эта ответит, что все отлично работает с нереактивной таблицей (см. mydata <- mtcars[1:5,]
). Но это не работает, когда mydata
становится реактивным!
Вот воспроизводимый рабочий пример с НЕРЕАКТИВНЫМИ данными из @ dww ответ:
library(rhandsontable)
ui <- fluidPage(
h2("The mtcars data"),
rHandsontableOutput("mytable"),
textInput('NewCol', 'Enter new column name'),
radioButtons("type", "Column type:",
c("Integer" = "integer",
"Floating point" = "numeric",
"Text" = "character")),
actionButton("goButton", "Update Table")
)
server <- function(input, output) {
mydata <- mtcars[1:5,]
output$mytable = renderRHandsontable(df())
df <- eventReactive(input$goButton, {
if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
if (input$type == "integer") v1 <- integer(NROW(mydata))
if (input$type == "numeric") v1 <- numeric(NROW(mydata))
if (input$type == "character") v1 <- character(NROW(mydata))
newcol <- data.frame(v1)
names(newcol) <- input$NewCol
mydata <<- cbind(mydata, newcol)
}
rhandsontable(mydata, stretchH = "all")
}, ignoreNULL = FALSE)
observe(if (!is.null(input$mytable)) mydata <<- hot_to_r(input$mytable))
}
shinyApp(ui,server)
Я безуспешно пытался эти изменения внутри кода (в основном я изменил все mydata
для mydata()
):
server <- function(input, output) {
# mydata <- reactive({ }) #make mydata a reactive object
output$mytable = renderRHandsontable(df())
df <- eventReactive(input$goButton, {
if(input$NewCol!="" && !is.null(input$NewCol) && input$goButton>0){
if (input$type == "integer") v1 <- integer(NROW(mydata()))
if (input$type == "numeric") v1 <- numeric(NROW(mydata()))
if (input$type == "character") v1 <- character(NROW(mydata()))
newcol <- data.frame(v1)
names(newcol) <- input$NewCol
mydata <<- cbind(mydata(), newcol)
}
rhandsontable(mydata(), stretchH = "all")
}, ignoreNULL = FALSE)
observe(if (!is.null(input$mytable)) mydata() <<- hot_to_r(input$mytable))}
Я не нашел это ответы на вопросы / комментарии, полезные для решения моей проблемы).
Не могли бы вы объяснить, как использовать реактивный mydata
внутри @dww потрясающего ответа?
[ПРАВИТЬ: названиеобновлено, чтобы лучше соответствовать ответу]