我构建了一个查询SQL数据库的闪亮应用程序,因此用户可以ggplot数据。我希望用户能够手动重命名因素,但我正在努力前进。下面是我想要做的一个例子:闪亮 - 反应数据框中的重命名因素
ui.R
library(markdown)
shinyUI(fluidPage(
titlePanel("Reactive factor label"),
sidebarLayout(
sidebarPanel(
numericInput("wafer", label = h3("Input wafer ID:"), value = NULL),
actionButton("do", "Search wafer"),
textInput("text", label = h3("Factor name to change"), value = ""),
textInput("text", label = h3("New factor name"), value = ""),
actionButton("do2", "Change name")
),
mainPanel(
verbatimTextOutput("waf"),
verbatimTextOutput("que"),
verbatimTextOutput("pos"),
dataTableOutput(outputId="tstat")
)
)
)
)
server.R
# Create example data
Name <- factor(c("Happy", "New", "Year"))
Id <- 1:3
dd <- data.frame(Id, Name)
con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "dd", dd)
query <- function(...) dbGetQuery(con, ...)
wq = data.frame()
sq = data.frame()
shinyServer(function(input, output){
# create data frame to store reactive data set from query
values <- reactiveValues()
values$df <- data.frame()
# Wait for user to search
d <- eventReactive(input$do, { input$wafer })
# Reactive SQL query
a <- reactive({ paste0("Select * from dd where Id=",d()) })
wq <- reactive({ query(a()) })
# Check outputs
output$waf <- renderPrint(input$wafer)
output$que <- renderPrint({ a() })
output$pos <- renderPrint(wq()[1,1])
# observe d() so that data is not added until user presses action button
observe({
if (!is.null(d())) {
sq <- reactive({ query(a()) })
# add query to reactive data frame
values$df <- rbind(isolate(values$df), sq())
}
})
output$tstat <- renderDataTable({
data <- values$df
})
})
在静态RI通常会使用数据表重命名因素,如:
DT <- data.table(df)
DT[Name=="Happy", Name:="Sad"]
但我不知道如何去做这个与reactiveValues即values$df
。
我已阅读此(R shiny: How to get an reactive data frame updated each time pressing an actionButton without creating a new reactive data frame?)。这导致我尝试这一点,但它不会做任何事情(即使没有错误):
observeEvent(input$do2, {
DT <- data.table(values$df)
DT[Name == input$text1, Name := input$text2]
values$df <- data.frame(values$df)
})
或许有围绕this..maybe的方式存在使用动作按钮“锁定”的方式数据作为一个新的数据框架,然后可以用它来重命名?
对不起这么长时间的问题。我的真实应用程序更长,更复杂。我试图剥夺它。
可能相关:http://stackoverflow.com/q/20201070/2679518如果你的'data.frame'本身就是一个被动对象,你将无法改变'data.frame'本身的名字。 –